#include "Camac_CC32.h"

Camac_CC32::Camac_CC32(char *path) : handle(0), q(0), x(0)
{
  Open(path);
};


Camac_CC32::~Camac_CC32()
{
  Close();
}


void Camac_CC32::Open(char *path)
{
  cc32_open(path, &handle);
}


void Camac_CC32::Close()
{
  if (handle)
    cc32_close(handle);
  handle = 0;
}


uint16_t Camac_CC32::ReadWord(unsigned int n, unsigned int a, unsigned int f)
{
  uint16_t data = 0;
  q = x = 0;
  if (handle)
    data = cc32_read_word_qx(handle, n, a, f, &q, &x);
  VerifySuccess();
  return data;
}


void Camac_CC32::WriteWord(unsigned int n, unsigned int a, unsigned int f, uint16_t data)
{
  q = x = 0;
  if (handle)
    cc32_write_word_qx(handle, n, a, f, data, &q, &x);
}


uint32_t Camac_CC32::ReadLong(unsigned int n, unsigned int a, unsigned int f)
{
  uint32_t data = 0;
  q = x = 0;
  if (handle)
    data = cc32_read_long_qx(handle, n, a, f, &q, &x);
  VerifySuccess();
  return data;
}


void Camac_CC32::WriteLong(unsigned int n, unsigned int a, unsigned int f, uint32_t data)
{
  q = x = 0;
  if (handle)
    cc32_write_long_qx(handle, n, a, f, data, &q, &x);
  VerifySuccess();
}


void Camac_CC32::VerifySuccess()
{
  if (!q || !x) 
    throw "CAMAC access error!";
}

